package com.tuanzi.loan.web.bean.system;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.system.Announcement;
import com.tuanzi.loan.business.service.system.AnnouncementService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class AnnouncementInfoBean extends JSFInfoBean<Announcement> {

	private static final long serialVersionUID = -2152778554108785342L;
	@Autowired
	private AnnouncementService announcementService;

	@PostConstruct
	private void init() {
		String id = getParamFromRequest("id");
		if (StringUtils.isNotBlank(id)) {
			$ = announcementService.findOne(id);
		} else {
			$ = new Announcement();
			changeStatusToAdd();
		}
	}

	public void onSave() {
		$ = announcementService.saveOrUpdate($);
		changeStatusToView();
		addSuccessfullyMessage();
	}

}
